package org.xtremeobjects.domain.hibernate;

import org.hibernate.*;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {

    static {
    	AnnotationConfiguration config = new AnnotationConfiguration();
    	try /**/{
    		config.
    		addAnnotatedClass(Class.forName("org.xtremeobjects.domain.hibernate.TestObject")).
          	addAnnotatedClass(Class.forName("org.xtremeobjects.domain.person.Person")).
        	addAnnotatedClass(Class.forName("org.xtremeobjects.domain.person.ContactDetail")).
        	addAnnotatedClass(Class.forName("org.xtremeobjects.domain.person.Phone")).
        	addAnnotatedClass(Class.forName("org.xtremeobjects.domain.person.Address")).
        	addAnnotatedClass(Class.forName("org.xtremeobjects.domain.person.Email")).
        	addAnnotatedClass(Class.forName("org.xtremeobjects.domain.person.Sex")).
        	addAnnotatedClass(Class.forName("org.xtremeobjects.domain.party.Party")).
        	addAnnotatedClass(Class.forName("org.xtremeobjects.domain.organization.Organization"));
//        	addPackage("org.xtremeobjects.domain.person").
    		/**/
    	} catch (Exception ex) {
    		ex.printStackTrace();
    	}/**/

    	/**/
    	config.
    	setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect").
        setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver").
        setProperty("hibernate.connection.url", "jdbc:hsqldb:file:/xtremeobjects_dev").
//            setProperty("hibernate.connection.url", "jdbc:hsqldb:mem:xtremeobjects_dev").
        setProperty("hibernate.connection.username", "sa").
        setProperty("hibernate.connection.password", ""). /**/

/*    	setProperty("hibernate.dialect", "org.hibernate.dialect.SQLServerDialect").
        setProperty("hibernate.connection.driver_class", "com.microsoft.sqlserver.jdbc.SQLServerDriver").
        setProperty("hibernate.connection.url", "jdbc:sqlserver://127.0.0.1:1433;;databaseName=xtremeobjects_dev;SelectMethod=cursor;").
        setProperty("hibernate.connection.username", "develop").
        setProperty("hibernate.connection.password", "power3"). /**/

        setProperty("hibernate.connection.pool_size", "1").
        setProperty("hibernate.connection.autocommit", "true").
        setProperty("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider").
        setProperty("hibernate.hbm2ddl.auto", "update").
        setProperty("hibernate.show_sql", "true")
        ;
        setSessionFactory(config.buildSessionFactory());

    }
    private static SessionFactory factory;

    public static synchronized Session getSession() {
        if (factory == null) {
            factory = new AnnotationConfiguration().configure().buildSessionFactory();
        }
        return factory.openSession();
    }

    public static void setSessionFactory(SessionFactory factory) {
        HibernateUtil.factory = factory;
    }

    public static void showConsole()
    {
    	new org.hsqldb.DatabaseManager();

    }
}
